#include "sqlconnpool.h"

SqlConnPool::SqlConnPool() {

}

SqlConnPool::~SqlConnPool() {
    
}

SqlConnPool* SqlConnPool::Instance() {
    static SqlConnPool instance;
    return &instance;
}

void SqlConnPool::Init(const char* host, int port, 
        const char* user, const char* pwd, 
        const char* dbname, int connsize) {
    assert(connsize > 0);
    for (int i = 0; i < connsize; ++i) {
        MYSQL *sql = nullptr;
        sql = mysql_init(sql); // 初始化数据库  
        if (!sql) {
            // 初始化失败
            LOG_ERROR("mysql init error");
            assert(sql);
        }      
        sql = mysql_real_connect(sql, host, user, pwd, dbname, port, nullptr, 0);
        if (!sql) {
            // 连接失败
            LOG_ERROR("mysql connect error");
        }
        MAX_CONN = connsize;
        sem_init(&m_semId, 0, MAX_CONN);
    }
}

void SqlConnPool::ClosePool() {

}